package com.houtai.aclservice.controller;


import com.houtai.aclservice.entity.AclPermission;
import com.houtai.aclservice.service.AclPermissionService;
import com.houtai.commonutils.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-12-11
 */
@RestController
@RequestMapping("/admin/acl/permission")
//@CrossOrigin
public class AclPermissionController {

    @Autowired
    public AclPermissionService aclPermissionService;
    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public Result indexAllPermission(){
        List<AclPermission> list=aclPermissionService.queryAllMenu();
        return Result.ok().data("children",list);
    }
    //递归删除菜单
    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id){
        aclPermissionService.removeChildById(id);
        return Result.ok();
    }
    //给角色分配权限
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result deAssign(String roleId,String[] permissionId){
        aclPermissionService.saveRolePermission(roleId,permissionId);
        return Result.ok();
    }
    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId){
        List<AclPermission> list=aclPermissionService.selectAllMenu(roleId);
        return Result.ok().data("children",list);
    }
    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public Result save(@RequestBody AclPermission permission) {
        aclPermissionService.save(permission);
        return Result.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public Result updateById(@RequestBody AclPermission permission) {
        aclPermissionService.updateById(permission);
        return Result.ok();
    }

}

